package com.example.linyiquan.zxjt;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**智能交通
 *
 */
public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    private Spinner sp_content;
    private EditText et_IP;
    private EditText et_port;
    private Button bt_execute;
    private TextView tv_content;

    private String ip;
    private String port;
    private String[] actionName = {
            "GetCarSpeed",
            "SetCarMove",
            "GetCarAccountBalance",
            "SetCarAccountRecharge",
            "GetTrafficLightConfigAction",
            "SetParkRate",
            "GetParkRate",
            "GetParkFree",
            "GetAllSense",
            "GetLightSenseValve",
            "GetBusStationInfo",
            "GetRoadStatus"
    };

    private String strUrl;
    private EditText et_carid;
    private EditText et_tralightid;
    private EditText et_retetype;
    private EditText et_money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyVolley.init(this);
        initView();
    }

    private void initView() {
        sp_content = (Spinner) findViewById(R.id.sp_content);
        sp_content.setOnItemSelectedListener(this);
        et_IP = (EditText) findViewById(R.id.et_IP);
        et_port = (EditText) findViewById(R.id.et_port);
        bt_execute = (Button) findViewById(R.id.bt_execute);
        bt_execute.setOnClickListener(this);
        tv_content = (TextView) findViewById(R.id.tv_content);
    }

    @Override
    public void onClick(View v) {

        ip = et_IP.getText().toString();
        port = et_port.getText().toString();
        isIP(ip);

        if (TextUtils.isEmpty(ip)) {
            Toast.makeText(this, "ip地址不能为空", Toast.LENGTH_SHORT).show();
            return;
        }


        if (TextUtils.isEmpty(port)) {
            Toast.makeText(this, "端口号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        strUrl = "http://" + ip + ":" + port
                + "/transportservice/type/jason/action/" + actionName[sp_content.getSelectedItemPosition()] + ".do";

        final Map map=new HashMap();


        LayoutInflater inflater = getLayoutInflater();
        View layout;
        AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

        switch (sp_content.getSelectedItemPosition()) {
            case 0:
                layout = inflater.inflate(R.layout.carid, (ViewGroup) findViewById(R.id.dialog));
                alert.setTitle("查询小车当前速度").setView(layout);
                et_carid = (EditText) layout.findViewById(R.id.et_carid);
                alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String carid = et_carid.getText().toString();
                        map.put("CarId",carid);

                        JSONObject jsonObject =new JSONObject(map);

                        MyVolley.Post(strUrl,jsonObject , new MyVolley.OnRequestLitener() {
                            @Override
                            public void done(JSONObject request) {
                                tv_content.setText(request.toString());
                                System.out.println(request.toString());
                            }

                            @Override
                            public void fail(VolleyError error) {
                                System.out.println(error);
                            }
                        });
                    }
                });
                alert.setNegativeButton("取消", null).show();
                break;
            case 1:
                layout = inflater.inflate(R.layout.caraction, (ViewGroup) findViewById(R.id.dialog));
                alert.setTitle("设置小车动作").setView(layout);
                et_carid = (EditText) layout.findViewById(R.id.et_carid);
                final EditText et_caraction = (EditText) layout.findViewById(R.id.et_caraction);
                alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String carid = et_carid.getText().toString();
                        String caraction = et_caraction.getText().toString();
                        map.put("CarId",carid);
                        map.put("CarAction",caraction);
                        JSONObject jsonObject=new JSONObject(map);

                        MyVolley.Post(strUrl, jsonObject, new MyVolley.OnRequestLitener() {
                            @Override
                            public void done(JSONObject request) {
                                tv_content.setText(request.toString());
                            }

                            @Override
                            public void fail(VolleyError error) {
                                System.out.println(error);
                            }
                        });

                    }
                });
                alert.setNegativeButton("取消",null).show();
                break;
            case 2:
                layout = inflater.inflate(R.layout.carid, (ViewGroup) findViewById(R.id.dialog));
                alert.setTitle("查询小车账户余额").setView(layout);
                et_carid= (EditText) layout.findViewById(R.id.et_carid);
                alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String carid=et_carid.getText().toString();
                        map.put("CarId",carid);
                        JSONObject jsonObject=new JSONObject(map);
                        MyVolley.Post(strUrl, jsonObject, new MyVolley.OnRequestLitener() {
                            @Override
                            public void done(JSONObject request) {
                                tv_content.setText(request.toString());
                            }

                            @Override
                            public void fail(VolleyError error) {
                                System.out.println(error);
                            }
                        });
                    }
                });
                alert.setNegativeButton("取消",null).show();
                break;
            case 3:
                layout=inflater.inflate(R.layout.caraccountrecharge, (ViewGroup) findViewById(R.id.dialog));
                alert.setTitle("小车账户充值").setView(layout);
                et_carid= (EditText) layout.findViewById(R.id.et_carid);
                final EditText et_carmoney= (EditText) layout.findViewById(R.id.et_carmoney);
                alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String carid=et_carid.getText().toString();
                        String carmoney=et_carmoney.getText().toString();
                        map.put("CarId",carid);
                        map.put("Money",carmoney);
                        JSONObject jsonObject=new JSONObject(map);
                        MyVolley.Post(strUrl, jsonObject, new MyVolley.OnRequestLitener() {
                            @Override
                            public void done(JSONObject request) {
                                tv_content.setText(request.toString());
                            }

                            @Override
                            public void fail(VolleyError error) {
                                System.out.println(error);
                            }
                        });
                    }
                });
                alert.setNegativeButton("取消",null).show();
                break;
            case 4:
                layout=inflater.inflate(R.layout.trafficlight, (ViewGroup) findViewById(R.id.dialog));
                alert.setTitle("查询红绿灯的配置信息:").setView(layout);
                et_tralightid= (EditText) layout.findViewById(R.id.et_trafficlight);
                alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String tralightid=et_tralightid.getText().toString();
                        map.put("TrafficLightID",tralightid);
                        JSONObject jsonObject=new JSONObject(map);
                        MyVolley.Post(strUrl, jsonObject, new MyVolley.OnRequestLitener() {
                            @Override
                            public void done(JSONObject request) {
                                tv_content.setText(request.toString());
                            }

                            @Override
                            public void fail(VolleyError error) {
                                System.out.println(error);
                            }
                        });
                    }
                });
                alert.setNegativeButton("取消",null).show();
                break;
            case 5:
                layout=inflater.inflate(R.layout.setrate, (ViewGroup) findViewById(R.id.dialog));
                alert.setTitle("停车场费率设置").setView(layout);
                et_money= (EditText) layout.findViewById(R.id.et_money);
                et_retetype= (EditText) layout.findViewById(R.id.et_ratetype);
                alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String ratetype=et_retetype.getText().toString();
                        String money=et_money.getText().toString();
                        map.put("RateType",ratetype);
                        map.put("Money",money);
                        JSONObject jsonObject=new JSONObject(map);
                        MyVolley.Post(strUrl, jsonObject, new MyVolley.OnRequestLitener() {
                            @Override
                            public void done(JSONObject request) {
                                tv_content.setText(request.toString());
                            }

                            @Override
                            public void fail(VolleyError error) {
                                System.out.println(error);
                            }
                        });
                    }
                });
                alert.setNegativeButton("取消",null).show();
                break;
            case 6:
                MyVolley.Post(strUrl, null, new MyVolley.OnRequestLitener() {
                    @Override
                    public void done(JSONObject request) {
                        tv_content.setText(request.toString());
                    }

                    @Override
                    public void fail(VolleyError error) {
                        System.out.println(error);
                    }
                });
                break;
            case 7:
                MyVolley.Post(strUrl, null, new MyVolley.OnRequestLitener() {
                    @Override
                    public void done(JSONObject request) {
                        tv_content.setText(request.toString());
                    }

                    @Override
                    public void fail(VolleyError error) {
                        System.out.println(error);
                    }
                });
                break;
            case 8:
                MyVolley.Post(strUrl, null, new MyVolley.OnRequestLitener() {
                    @Override
                    public void done(JSONObject request) {
                        tv_content.setText(request.toString());
                    }

                    @Override
                    public void fail(VolleyError error) {
                        System.out.println(error);
                    }
                });
                break;
            case 9:
                MyVolley.Post(strUrl, null, new MyVolley.OnRequestLitener() {
                    @Override
                    public void done(JSONObject request) {
                        tv_content.setText(request.toString());
                    }

                    @Override
                    public void fail(VolleyError error) {
                        System.out.println(error);
                    }
                });
                break;
            case 10:
                layout=inflater.inflate(R.layout.bussstation, (ViewGroup) findViewById(R.id.dialog));
                alert.setTitle("站台信息查询").setView(layout);
                final EditText et_busstationid= (EditText) layout.findViewById(R.id.et_busstationid);
                alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String busstationid=et_busstationid.getText().toString();
                        map.put("BusStationID",busstationid);
                        JSONObject jsonObject=new JSONObject(map);
                        MyVolley.Post(strUrl, jsonObject, new MyVolley.OnRequestLitener() {
                            @Override
                            public void done(JSONObject request) {
                                tv_content.setText(request.toString());
                            }

                            @Override
                            public void fail(VolleyError error) {
                                System.out.println(error);
                            }
                        });
                    }
                });
                alert.setNegativeButton("取消",null).show();
                break;
            case 11:
                layout=inflater.inflate(R.layout.road, (ViewGroup) findViewById(R.id.dialog));
                alert.setTitle("查询道路状况").setView(layout);
                final EditText et_roadid= (EditText) layout.findViewById(R.id.et_roadid);
                alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String roadid=et_roadid.getText().toString();
                        map.put("RoadID",roadid);
                        JSONObject jsonObject=new JSONObject(map);
                        MyVolley.Post(strUrl, jsonObject, new MyVolley.OnRequestLitener() {
                            @Override
                            public void done(JSONObject request) {
                                tv_content.setText(request.toString());
                            }

                            @Override
                            public void fail(VolleyError error) {
                                System.out.println(error);
                            }
                        });
                    }
                });
                alert.setNegativeButton("取消",null).show();
                break;
        }


    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    public boolean isIP(String addr) {
        if (addr.length() < 7 || addr.length() > 15 || "".equals(addr)) {
            return false;
        }
        /**
         * 判断IP格式和范围
         */
        String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
        Pattern pat = Pattern.compile(rexp);
        Matcher mat = pat.matcher(addr);
        boolean ipAddress = mat.find();
        //============对之前的ip判断的bug在进行判断
        if (ipAddress == true) {
            String ips[] = addr.split("\\.");
            if (ips.length == 4) {
                try {
                    for (String ip : ips) {
                        if (Integer.parseInt(ip) < 0 || Integer.parseInt(ip) > 255) {
                            return false;
                        }
                    }
                } catch (Exception e) {
                    return false;
                }
                return true;
            } else {
                return false;
            }
        }
        Toast.makeText(this, "ip地址不合法", Toast.LENGTH_SHORT).show();
        return ipAddress;

    }

//    protected boolean limitId(String string, int limit, String[] msg) {
//        if (string.equals("") || string == null) {
//            Toast.makeText(MainActivity.this, msg[0], Toast.LENGTH_SHORT).show();
//            return false;
//        }
//        int id = Integer.valueOf(string);
//        if (!(0 < id && id <= limit)) {
//            Toast.makeText(MainActivity.this, msg[1], Toast.LENGTH_SHORT).show();
//            return false;
//        }
//        return true;
//    }

}


